/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.platform.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.platform.entity.DeviceTable;
import org.springblade.modules.platform.entity.ProductModelProperties;
import org.springblade.modules.platform.mapper.DeviceTableMapper;
import org.springblade.modules.platform.mapper.ProductModelPropertiesMapper;
import org.springblade.modules.platform.service.IProductModelPropertiesService;
import org.springblade.modules.platform.vo.ProductModelPropertiesVO;
import org.springblade.modules.platform.vo.PropertiesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2020-11-17
 */
@Service
public class ProductModelPropertiesServiceImpl extends ServiceImpl<ProductModelPropertiesMapper, ProductModelProperties> implements IProductModelPropertiesService {

	@Autowired
	DeviceTableMapper deviceTableMapper;

	@Override
	public IPage<ProductModelPropertiesVO> selectProductModelPropertiesPage(IPage<ProductModelPropertiesVO> page, ProductModelPropertiesVO productModelProperties) {
		return page.setRecords(baseMapper.selectProductModelPropertiesPage(page, productModelProperties));
	}

	@Override
	public List<PropertiesVO> getPropertiesList(String equipmentId) {

		List<PropertiesVO>  list = new ArrayList<>();
		DeviceTable one = deviceTableMapper.selectOne(Wrappers.<DeviceTable>query().lambda()
				.eq(DeviceTable::getDeviceId,equipmentId).eq(DeviceTable::getIsDeleted, 0));


		ProductModelProperties productModelProperties = baseMapper.selectOne(Wrappers.<ProductModelProperties>query().lambda()
				.eq(ProductModelProperties::getProductId, one.getProductId()));
		if(StringUtils.isBlank(productModelProperties.getPropertiesJson())){
			return list;
		}
		JSONArray jsonArray = JSONArray.parseArray(productModelProperties.getPropertiesJson());
		for (Object o : jsonArray) {
			JSONObject o1 = (JSONObject) o;
			PropertiesVO propertiesVO = new PropertiesVO();
			propertiesVO.setKey(o1.get("id").toString());
			propertiesVO.setName(o1.get("name").toString());
			list.add(propertiesVO);
		}
		return list;
	}

}
